//build panel structure. bank x year
clear 
set obs 16
gen year = _n+2005
tempfile years 
save `years'

use ../Intermediate/bank_ban_strength_yearly, clear
keep BankID 
duplicates drop
cross using `years'
merge 1:1 BankID year using ../Intermediate/bank_ban_strength_yearly, keep(1 3) nogen   //extract bank ban policies from 2006 to 2021


egen hcp = max(has_coded_policy), by(BankID) //considering the missing years from 2006-2015
drop has_coded_policy
rename hcp has_coded_policy
egen fby = max(first_ban_year), by(BankID)
drop first_ban_year
rename fby first_ban_year

replace active_policy = 0 if missing(active_policy)
replace ban_intensity = 0 if missing(ban_intensity) 
replace ban_intensity_new1 = 0 if missing(ban_intensity_new1) 

tempfile bankyear 
save `bankyear'


* Get underlying transaction data 
use ../Intermediate/transaction_clean, clear
//merge in the lenders for each instrument
joinby transaction_id using ../Intermediate/transaction_lender_clean
//merge in borrower info
merge m:1 borrower_id using ../Intermediate/borrower_chars, keep(1 3) nogen keepusing(Country Continent)

gen year = yofd(date_trans)
drop if amount_trans_usdm <= 0 | missing(amount_trans_usdm) | amount_trans_lender_usdm < 0
drop if missing(date_trans)
//collapse to bank year

gen debt = amount_trans_lender_usdm*(obs_ammendment==0)
gen loan = amount_trans_lender_usdm*(obs_ammendment==0)*(bond_ind==0)
gen bond = amount_trans_lender_usdm*(obs_ammendment==0)*(bond_ind==1)

gen debt_nc = amount_trans_lender_usdm*(obs_ammendment==0)*(Country~="China")
gen loan_nc = amount_trans_lender_usdm*(obs_ammendment==0)*(bond_ind==0)*(Country~="China")
gen bond_nc = amount_trans_lender_usdm*(obs_ammendment==0)*(bond_ind==1)*(Country~="China")

gen debt_wa = amount_trans_lender_usdm
gen loan_wa = amount_trans_lender_usdm*(bond_ind==0)
gen bond_wa = amount_trans_lender_usdm*(bond_ind==1)


gen debt_na = amount_trans_lender_usdm*(obs_ammendment==0)*(Continent=="North America")
gen loan_na = amount_trans_lender_usdm*(obs_ammendment==0)*(bond_ind==0)*(Continent=="North America")
gen bond_na = amount_trans_lender_usdm*(obs_ammendment==0)*(bond_ind==1)*(Continent=="North America")

gen debt_eur = amount_trans_lender_usdm*(obs_ammendment==0)*(Continent=="Europe")
gen loan_eur = amount_trans_lender_usdm*(obs_ammendment==0)*(bond_ind==0)*(Continent=="Europe")
gen bond_eur = amount_trans_lender_usdm*(obs_ammendment==0)*(bond_ind==1)*(Continent=="Europe")


gen debt_asia = amount_trans_lender_usdm*(obs_ammendment==0)*(Continent=="Asia")
gen loan_asia = amount_trans_lender_usdm*(obs_ammendment==0)*(bond_ind==0)*(Continent=="Asia")
gen bond_asia = amount_trans_lender_usdm*(obs_ammendment==0)*(bond_ind==1)*(Continent=="Asia")



collapse (sum) debt loan bond debt_nc loan_nc bond_nc debt_na loan_na bond_na debt_eur loan_eur bond_eur debt_asia loan_asia bond_asia *_wa, by(BankID year)

merge 1:1 BankID year using `bankyear', keep(2 3) 
gen no_lending_bank_year = _merge == 2
drop _merge

foreach var of var debt loan bond debt_nc loan_nc bond_nc debt_* bond_* loan_* {
        replace `var' = 0 if no_lending_bank_year == 1
}  



//look at total coal volume by bank
egen total_coal_vol = sum(debt), by(BankID)
egen ban_intensity_max = max(ban_intensity), by(BankID)
egen ban_intensity_initial_max = max(ban_intensity_initial), by(BankID)
egen ban_intensity_new1_max = max(ban_intensity_new1), by(BankID)

bys BankID: egen total_ban=total(ban_intensity)
gen strong_ban=(total_ban>0.995918 )


preserve
keep if year == 2020
keep BankID total_coal_vol* ban_intensity* ban_intensity_new1* has_coded_policy active_policy
merge 1:1 BankID using ../Raw/Bank_Sample/BankSample_List, keep(3) nogen
gsort -total_coal_vol
gen rank = _n 
gen top100 = rank <= 100
tab has_coded_policy top100
rename ban_intensity ban_intensity2020
rename active_policy active_policy2020
gen strong_policy2020 = ban_intensity2020 > 0.29
gen medium_policy2020 = ban_intensity2020 <= 0.29 & has_coded_policy == 1
gen no_policy2020 = has_coded_policy == 0

gen strong_policy_max = ban_intensity_max > 0.42
gen medium_policy_max = ban_intensity_max <= 0.42 & has_coded_policy == 1
gen no_policy_max = has_coded_policy == 0

gen strong_policy_new1_max = ban_intensity_new1_max > 0.57
gen medium_policy_new1_max = ban_intensity_new1_max <= 0.57 & has_coded_policy == 1
gen no_policy_new1_max = has_coded_policy == 0

keep BankID total_coal_vol* rank ban_intensity2020 active_policy2020 ban_intensity_max top100 strong_* medium_* no_policy* ban_intensity_max ban_intensity_new1_max

tempfile cx 
save `cx'

restore

merge m:1 BankID using `cx', keep(3) nogen
save ../Intermediate/bank_lending_panel, replace
